﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using UcAsp.Net.Extract.Plug.Trends.Model;
namespace UcAsp.Net.Extract.Plug.Trends.BLL
{
    class Config
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        public static List<Model.Config> GetListConfig
        {
            get
            {
                List<Model.Config> list = new List<Model.Config>();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\config\\Trends\\Config.config");
                string xmlpath = ("Root/Defined");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {

                        Model.Config model = new Model.Config();

                        foreach (XmlNode xn in xns)
                        {

                            XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                return list;
            }
        }
        protected static void XmlToObject(object oPut, string name, string value)
        {
            System.ComponentModel.PropertyDescriptor pd = System.ComponentModel.TypeDescriptor.GetProperties(oPut).Find(name, true);
            if (pd != null)
            {
                if (pd.PropertyType == typeof(int) || pd.PropertyType == typeof(long) || pd.PropertyType == typeof(string) || pd.PropertyType == typeof(DateTime) || pd.PropertyType == typeof(bool))
                {
                    if (pd.PropertyType == typeof(int))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt32(value));
                    }
                    else if (pd.PropertyType == typeof(long))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt64(value));
                    }
                    else if (pd.PropertyType == typeof(double))
                    {
                        pd.SetValue(oPut, System.Convert.ToDouble(value));
                    }
                    else if (pd.PropertyType == typeof(bool))
                    {
                        pd.SetValue(oPut, System.Convert.ToBoolean(value));
                    }
                    else
                    {
                        if (value == null)
                            pd.SetValue(oPut, string.Empty);
                        else
                            pd.SetValue(oPut, value);
                    }
                }
            }
            else
            {
                //对象为空
            }
        }
        public static Model.Config GetDefined(string definedname)
        {
            return GetListConfig.FirstOrDefault(o => o.DefinedName == definedname);
        }
        public static bool UpdateConfig(Model.Config model)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\config\\Trends\\Config.config");
            string xmlpath = ("Root/Defined");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {

                if (xns["DefinedName"].InnerText == model.DefinedName)
                {
                    xns["DefinedTitle"].InnerText = model.DefinedTitle;

                }
            }
            xmlDoc.Save(path + "\\config\\Trends\\Config.config");
            return true;
        }
        public static bool InsertConfig(Model.Config model)
        {

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\config\\Trends\\Config.config");
            string xmlpath = ("Root");
            XmlNode root = xmlDoc.SelectSingleNode(xmlpath);
            XmlElement element = xmlDoc.CreateElement("Defined");

            XmlElement DefinedName = xmlDoc.CreateElement("DefinedName");
            DefinedName.InnerText = model.DefinedName;
            element.AppendChild(DefinedName);
            XmlElement DefinedTitle = xmlDoc.CreateElement("DefinedTitle");
            DefinedTitle.InnerText = model.DefinedTitle;
            element.AppendChild(DefinedTitle);

            root.AppendChild(element);
            xmlDoc.Save(path + "\\config\\Trends\\Config.config");
            return true;
        }
    }
}
